/*
 * Copyright (C) 2018 E.S.R.Labs
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <gtest/gtest.h>
#include <mindroid/util/Base64.h>

using namespace mindroid;

static const char* const testData[][2] = {
    { "", "" },
    { "f", "Zg==" },
    { "fo", "Zm8=" },
    { "foo", "Zm9v" },
    { "foob", "Zm9vYg==" },
    { "fooba", "Zm9vYmE=" },
    { "foobar", "Zm9vYmFy" },
    { "Mindroid", "TWluZHJvaWQ=" }
};

const uint8_t binaryTestData[] = {
    0xE2, 0xF2, 0xFC, 0x47, 0x40, 0xA0, 0xD1, 0x5A, 0xE5, 0xE3, 0x71, 0x46, 0xD7, 0xE0, 0x8C, 0xE8,
    0xA8, 0x40, 0x43, 0x6C, 0x1C, 0x32, 0x2F, 0xDB, 0xF9, 0x43, 0x7C, 0xC6, 0x23, 0x91, 0xB8, 0x3A,
    0x12, 0xB7, 0x1A, 0xBC, 0x35, 0x51, 0x75, 0x71, 0xD5, 0x43, 0x07, 0xC6, 0xF5, 0x6B, 0x01, 0x71,
    0xCB, 0x4D, 0x0C, 0x5D, 0x99, 0x59, 0x8F, 0xA3, 0x0F, 0xE5, 0xCE, 0xCE, 0xEB, 0x44, 0x3D, 0x97,
    0xA0, 0x6E, 0xF0, 0x16, 0xF3, 0x4B, 0x85, 0x78, 0xB8, 0x8F, 0xD2, 0xA0, 0xA7, 0x06, 0x1F, 0x9C,
    0x69, 0x50, 0x9A, 0xFB, 0x43, 0x93, 0x3D, 0xD9, 0xF7, 0x9F, 0x15, 0x11, 0x54, 0x12, 0x41, 0x1B,
    0xCA, 0xEB, 0x0A, 0xFF, 0xFA, 0xB9, 0x4A, 0x58, 0x5E, 0x8B, 0x8E, 0x7C, 0x86, 0x39, 0xBA, 0xBA,
    0xB7, 0x5F, 0x9A, 0x04, 0x8F, 0x46, 0x4B, 0xCE, 0xE7, 0x00, 0x09, 0x16, 0x45, 0xDD, 0xC4, 0x18,
    0xFA, 0xDF, 0xCE, 0x66, 0x65, 0x3B, 0xAE, 0x3F, 0x95, 0xD0, 0xC4, 0xB6, 0x7B, 0xBC, 0xAD, 0x2E,
    0xCD, 0x67, 0x80, 0x36, 0xE9, 0x5B, 0x5C, 0x54, 0x34, 0x7C, 0x6E, 0xAD, 0x47, 0x70, 0xD8, 0x1D,
    0x77, 0x76, 0xEA, 0x25, 0xF5, 0xFD, 0x46, 0xE1, 0xB0, 0xFC, 0xA1, 0xAA, 0x19, 0x95, 0xB9, 0x26,
    0x12, 0x1A, 0x3F, 0x39, 0x6C, 0x72, 0xF2, 0xFC, 0xB4, 0xC7, 0x7C, 0xFF, 0x17, 0x86, 0x0C, 0xFB,
    0x20, 0x3C, 0x68, 0x29, 0x3D, 0x48, 0x10, 0x5F, 0x30, 0xDE, 0xA0, 0xC8, 0xD6, 0x9C, 0x9F, 0xAB,
    0xAA, 0x7B, 0xEF, 0xEE, 0x47, 0xC5, 0xF9, 0x5F, 0x87, 0xA1, 0x7A, 0xA7, 0x1F, 0x23, 0x92, 0x19,
    0x12, 0x56, 0xB0, 0x6F, 0x4B, 0xD6, 0xC6, 0xCE, 0x0B, 0x72, 0x82, 0xBE, 0x28, 0x29, 0x9D, 0x50,
    0x73, 0x7A, 0xD3, 0x7A, 0xD7, 0xBA, 0x39, 0x53, 0x31, 0x7E, 0x7F, 0x27, 0x5F, 0x5D, 0x29, 0xA5
};

const char* const base64TestData =
    "4vL8R0Cg0Vrl43FG1+CM6KhAQ2wcMi/b+UN8xiORuDoStxq8NVF1cdVDB8b1aw"
    "Fxy00MXZlZj6MP5c7O60Q9l6Bu8BbzS4V4uI/SoKcGH5xpUJr7Q5M92fefFRFU"
    "EkEbyusK//q5Slhei458hjm6urdfmgSPRkvO5wAJFkXdxBj6385mZTuuP5XQxL"
    "Z7vK0uzWeANulbXFQ0fG6tR3DYHXd26iX1/UbhsPyhqhmVuSYSGj85bHLy/LTH"
    "fP8Xhgz7IDxoKT1IEF8w3qDI1pyfq6p77+5Hxflfh6F6px8jkhkSVrBvS9bGzg"
    "tygr4oKZ1Qc3rTete6OVMxfn8nX10ppQ==";

TEST(Mindroid, Base64Encoder) {
    sp<Base64::Encoder> encoder = Base64::getEncoder();

    auto encode = [=](const char* input) -> sp<String> {
        const sp<ByteArray> byteArray = String::valueOf(input)->getBytes();
        const sp<ArrayList<uint8_t>> arrayList = new ArrayList<uint8_t>(byteArray->c_arr(), byteArray->size());
        return encoder->encodeToString(arrayList);
    };

    for (auto &t : testData) {
        ASSERT_STREQ(encode(t[0])->c_str(), t[1]);
    }

    ASSERT_STREQ(base64TestData, encoder->encodeToString(new ArrayList<uint8_t>(binaryTestData, sizeof(binaryTestData)))->c_str());
}

TEST(Mindroid, Base64Decoder) {
    sp<Base64::Decoder> decoder = Base64::getDecoder();

    auto decode = [=](const char* input) -> sp<String> {
        const sp<ByteArray> byteArray = String::valueOf(input)->getBytes();
        const sp<ArrayList<uint8_t>> arrayList = new ArrayList<uint8_t>(byteArray->c_arr(), byteArray->size());
        auto decodedData = decoder->decode(arrayList);
        decodedData->add('\0');
        return new String(decodedData->c_arr(), decodedData->size());
    };

    for (auto &t : testData) {
        ASSERT_STREQ(t[0], decode(t[1])->c_str());
    }

    auto decodedData = decoder->decode(String::valueOf(base64TestData));
    for (size_t i = 0; i < sizeof(binaryTestData); ++i) {
        ASSERT_EQ(binaryTestData[i], decodedData->get(i));
    }
}
